package com.aWei.springbootinit.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.aWei.springbootinit.common.ErrorCode;
import com.aWei.springbootinit.exception.BusinessException;
import com.aWei.springbootinit.model.entity.Picture;
import com.aWei.springbootinit.service.PictureService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 小维哥
 * @version 1.2  图片服务实现类
 */
@Service
public class PictureServiceImpl implements PictureService {

        public String extractBaseUrl(String url) {
                try {
                        URL imageUrl = new URL(url);
                        String baseUrl = imageUrl.getProtocol() + "://" + imageUrl.getHost() + imageUrl.getPath();
                        return baseUrl;
                } catch (MalformedURLException e) {
                        // URL 格式错误，处理异常情况
                        e.printStackTrace();
                        return null;
                }
        }

        @Override
        public Page<Picture> searchPicture(String searchText, long pageSize, long pageNum) {
                if(searchText.isEmpty()){
                        searchText="洛天依";
                }
                long current = (pageNum - 1) * pageSize;
                int num = 1;
                String url = String.format("https://cn.bing.com/images/search?q=%s&form=HDRSC2&first=%d", searchText, current);
                Document doc = null;
                try {
                        doc = Jsoup.connect(url).get();
                } catch (IOException e) {
                        throw new BusinessException(ErrorCode.OPERATION_ERROR, "数据获取异常");
                }
                Elements newsHeadlines = doc.select(".iuscp.isv");
                List<Picture> picture = new ArrayList<>();
                for (Element index : newsHeadlines) {
                        String str = index.select(".iusc").get(0).attr("m");
                        Map<String, Object> map = JSONUtil.toBean(str, Map.class);
                        String resusr = (String) map.get("murl");
                        String baseUrl = extractBaseUrl(resusr);
                        String title = index.select(".inflnk").get(0).attr("aria-label") + (num++);
                        if (baseUrl.matches(".*jpg")) {
                                picture.add(new Picture(baseUrl, title));
                        }
                        if (picture.size() >= pageSize) {
                                break;
                        }
                }
                Page<Picture> picturePage = new Page<>(current, pageSize,99);
                picturePage.setRecords(picture);
                return picturePage;
        }
}
